package alingrad.engine.graphics.primitives;

import javax.microedition.khronos.opengles.GL10;

import alingrad.engine.collisions.CollisionMaskData;
import alingrad.engine.graphics.RenderablePrimitive;
import alingrad.engine.util.GLColor;

public class RenderableRectangle extends RenderablePrimitive {

	public final float mWidth;
	public final float mHeight;
	private CollisionMaskData mCollisionMaskData;

	public RenderableRectangle(final float width, final float height) {
		super(false);
		mWidth = width;
		mHeight = height;
		final float halfWidth = width / 2;
		final float halfHeight = height / 2;
		mCollisionMaskData = new CollisionMaskData((int)-halfWidth, (int)-halfHeight, (int)width, (int)height);
		mVertices = new float[12]; {
			mVertices[0] = -halfWidth;
			mVertices[1] = halfHeight;
			mVertices[2] = 0f;
			mVertices[3] = -halfWidth;
			mVertices[4] = -halfHeight;
			mVertices[5] = 0f;
			mVertices[6] = halfWidth;
			mVertices[7] = -halfHeight;
			mVertices[8] = 0f;
			mVertices[9] = halfWidth;
			mVertices[10] = halfHeight;
			mVertices[11] = 0f;
		}
		mIndices = new short[6]; {
			mIndices[0] = 0;
			mIndices[1] = 1;
			mIndices[2] = 2;
			mIndices[3] = 0;
			mIndices[4] = 2;
			mIndices[5] = 3;
		}
		createBuffers();
	}

	public RenderableRectangle(final float left, final float bottom, final float width, final float height) {
		super(true);
		mWidth = width;
		mHeight = height;
		mX = left;
		mY = bottom;
		final float halfWidth = width / 2;
		final float halfHeight = height / 2;
		mCollisionMaskData = new CollisionMaskData((int)-halfWidth, (int)-halfHeight, (int)width, (int)height);
		mVertices = new float[12]; {
			mVertices[0] = -halfWidth;
			mVertices[1] = halfHeight;
			mVertices[2] = 0f;
			mVertices[3] = -halfWidth;
			mVertices[4] = -halfHeight;
			mVertices[5] = 0f;
			mVertices[6] = halfWidth;
			mVertices[7] = -halfHeight;
			mVertices[8] = 0f;
			mVertices[9] = halfWidth;
			mVertices[10] = halfHeight;
			mVertices[11] = 0f;
		}
		mIndices = new short[6]; {
			mIndices[0] = 0;
			mIndices[1] = 1;
			mIndices[2] = 2;
			mIndices[3] = 0;
			mIndices[4] = 2;
			mIndices[5] = 3;
		}
		createBuffers();
	}

	@Override
	public void draw(GL10 gl, final float x, final float y, final float rotation, final float scaleX,
			final float scaleY, final GLColor color) {
		gl.glPushMatrix(); {
			gl.glDisable(GL10.GL_TEXTURE_2D);
			gl.glColor4f(color.mRed, color.mGreen, color.mBlue, color.mAlpha);
			if (mIgnoreTransformations) {
				gl.glTranslatef(mX, mY, 0f);
			} else {
				gl.glTranslatef(x, y, 0);
				gl.glRotatef(rotation, 0, 0, 1);
				gl.glScalef(scaleX, scaleY, 1f);
			}
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
			gl.glDrawElements(GL10.GL_TRIANGLES, mIndices.length, GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glEnable(GL10.GL_TEXTURE_2D);
		}gl.glPopMatrix();
	}

	@Override
	public CollisionMaskData getData() {
		return mCollisionMaskData;
	}
}
